<?php
namespace App\Http\Controllers\Admin\Platform;

use App\Service\Admin\Platform\AccountService;
use Illuminate\Http\Request;

class AccountController2 extends BaseController
{
    //账户

    public function __construct(AccountService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['phone'] = $request->post('phone', '');
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        if ($pas['phone'] && !preg_match('/^1[3-9]\d{9}$/', $pas['phone'])) {
            return $this->toJson(-2, '手机号格式错误');
        }
        $res = $this->db->getIndex($pas, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //选项
    public function getOption()
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //添加
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['realname'] = $request->post('realname', ''); //姓名
        $pas['phone'] = $request->post('phone', ''); //手机号
        $pas['uname'] = $request->post('uname', ''); //账户名称
        $pas['lang_front'] = $request->post('lang_front', 0); //前端语言
        $pas['lang_back'] = $request->post('lang_back', 0); //后端语言
        $pas['try_day'] = $request->post('try_day', 0); //试用天数
        $pas['period'] = $request->post('period', 0); //有效天数
        $pas['user_limit'] = $request->post('acc_limit', 0); //累计账户上限
        if (mb_strlen($pas['realname']) < 2) {
            return $this->toJson(-2, '姓名不能少于2个字符');
        }
        if (mb_strlen($pas['uname']) < 2) {
            return $this->toJson(-2, '账户名不能少于2个字符');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $pas['phone'])) {
            return $this->toJson(-2, '手机号格式错误');
        }
        $pas['try_day'] = $this->db->getUserPeriodDay($pas['try_day']);
        $pas['period'] = $this->db->getUserPeriodDay($pas['period']);
        if (!$pas['user_limit']) {
            return $this->toJson(-2, '账户上限必须的');
        }
        $res = $this->db->add($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //审核拒绝|通过
    public function setAuth(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $auth = $request->post('auth', 0); //1申请中，2已拒绝，3已通过
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        if (!in_array($auth, [2, 3])) {
            return $this->toJson(-2, '审核错误');
        }
        $res = $this->db->setAuth($id, $auth);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function setDel(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //绑卡审核列表
    public function bankList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $phone = $request->post('phone', '');
        $auth = $request->post('is_auth', 0); //1审核中，3已绑定
        if (!in_array($auth, [1, 3])) {
            return $this->toJson(-2, '状态值错误');
        }
        $res = $this->db->bankList($phone, $auth);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //绑卡审核
    public function bankAuth(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $auth = $request->post('is_auth', 0); //1拒绝，2通过
        if (!in_array($auth, [1, 2])) {
            return $this->toJson(-2, '状态值错误');
        }
        $res = $this->db->bankAuth($id, $auth);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}